package com.ccnf.web.controller;

import com.ccnf.core.bean.Response;
import com.ccnf.core.bean.ResponseWithData;
import com.ccnf.model.entity.RewardCash;
import com.ccnf.model.entity.RewardPrize;
import com.ccnf.service.AccountService;
import com.ccnf.service.RewardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "AccountController", description = "赏金账户相关API")
@RestController
@RequestMapping("/invitations")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private RewardService rewardService;

    @ApiOperation("获取用户邀请函账户信息")
    @GetMapping("{invitationId}/users/{userId}/account")
    public ResponseWithData invitationUserAccount(@PathVariable("invitationId") Long invitationId,
                                                  @PathVariable("userId") Long userId) {
        return ResponseWithData.success(accountService.findUserInvitationAccount(invitationId, userId));
    }

    @ApiOperation("获取用户邀请函账户信息")
    @GetMapping("accounts/{accountId}")
    public ResponseWithData invitationAccount(@PathVariable Long accountId) {
        return ResponseWithData.success(accountService.findInvitationAccountByAccountId(accountId));
    }

    @ApiOperation("获取用户邀请函账户收入明细")
    @GetMapping("accounts/{accountId}/income/detail")
    public ResponseWithData invitationAccountIncomeDetail(@PathVariable Long accountId) {
        return ResponseWithData.success(accountService.findAccountIncomeDetailByAccountId(accountId));
    }

    @ApiOperation("录入用户邀请函提现数据")
    @PostMapping("reward/cash")
    public ResponseWithData rewardCash(@RequestBody RewardCash rewardCash) {
        return ResponseWithData.success(accountService.saveRewardAccountCash(rewardCash));
    }

    @ApiOperation("修改用户邀请函提现数据")
    @PatchMapping("reward/cash/{cashId}")
    public Response rewardCash(@PathVariable Long cashId, @RequestBody RewardCash rewardCash) {
        rewardCash.setCashId(cashId);
        accountService.modifyRewardAccountCashByCashId(rewardCash);
        return Response.success();
    }

    @ApiOperation("获取用户邀请函账户提现明细")
    @GetMapping("accounts/{accountId}/cash/detail")
    public ResponseWithData invitationAccountCashDetail(@PathVariable Long accountId) {
        return ResponseWithData.success(accountService.findAccountCashDetailByAccountId(accountId));
    }

    @ApiOperation("获取用户奖品二维码")
    @GetMapping("{invitationId}/users/{userId}/prize")
    public ResponseWithData userPrize(@PathVariable("invitationId") Long invitationId,
                                      @PathVariable("userId") Long userId) {
        return ResponseWithData.success(rewardService.findUserPrize(invitationId, userId));
    }

    @ApiOperation("获取奖品中奖用户名单")
    @GetMapping("{invitationId}/prize/winner")
    public ResponseWithData winner(@PathVariable Long invitationId) {
        return ResponseWithData.success(rewardService.findWinnerUserList(invitationId));
    }

    @ApiOperation("获取奖品信息，用于验证奖品二维码有效性")
    @GetMapping("prizes/{prizeId}")
    public ResponseWithData getPrize(@PathVariable Long prizeId) {
        return ResponseWithData.success(rewardService.findPrizeByPrizeId(prizeId));
    }

    @ApiOperation("修改奖品信息")
    @PatchMapping("prizes/{prizeId}")
    public Response modifyPrize(@PathVariable Long prizeId, @RequestBody RewardPrize rewardPrize) {
        rewardPrize.setPrizeId(prizeId);
        rewardService.modifyPrizeByPrizeId(rewardPrize);
        return Response.success();
    }
}